package com.thinkfit.jy.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class CarTrackView extends View {
    private static final String TAG = "CarTrackView";
    //    private final static float SPEED_RATIO = 0.1f; //控制速度
    private float SPEED_RATIO = 0.00f; //控制速度8km/h = 0.04f
//    private static float SPEED_RATIO = 0.0001f; //控制速度 = 8km/h = 0.04f
    private static float SPEED_RATIO1 = 0.12f; //控制速度
//    private static float SPEED_RATIO1 = 0.0003f; //控制速度
    private static float SPEED_RATIO2 = 0.04f; //控制速度
    private static float SPEED_RATIO3 = 0.16f; //控制速度
    private static float SPEED_RATIO4 = 0.14f; //控制速度
//    private static float SPEED_RATIO2 = 0.0002f; //控制速度
    private static final int DEFAULT_TransColor = Color.argb(60, 255, 255, 255);
    private static final int DEFAULT_INNER_ARC_COLOR = Color.argb(100, 255, 255, 255);
    private static final int DEFAULT_ORANGE_COLOR = Color.argb(100, 254, 107, 0);

    private Paint contourPaint; //轮廓画笔
    private float distanceRatio = 0; //距离比例
    private float distanceRatio2 = 0; //距离比例
    private float distanceRatio3 = 0; //距离比例
    private float distanceRatio4 = 0; //距离比例
    private float distanceRatio5 = 0; //距离比例
    private Paint carPaint; //画小车的画笔
    private Paint carPaint1; //画小车的画笔
    private Paint carPaint2; //画小车的画笔
    private Context mContext; //画小车的画笔
    //进度点画笔
    private Paint mPaintProgressPoint;
//    private Paint mPaintProgressPoint1;
//    private Paint mPaintProgressPoint2;
//    private Paint mPaintProgressPoint3;
//    private Paint mPaintProgressPoint4;
    private float[] mPos = new float[2];
//    private float[] mPos1 = new float[2];
//    private float[] mPos2 = new float[2];
//    private float[] mPos3 = new float[2];
//    private float[] mPos4 = new float[2];
    private float mProgressPointRadius;
    int index = 0;
    int time = 0;
    public CarTrackView(Context context) {
        super(context);
        mContext= context;
        init();
    }

    public CarTrackView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext= context;
        init();
    }

    public CarTrackView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void changeSpeed(double speed){
        if(speed <= 0){
            return;
        }
       SPEED_RATIO = (float) ((speed/(double)8)*0.04);
       invalidate();
    }

    public void changeDistance(double distance){
//        SPEED_RATIO = (float) distance;
        SPEED_RATIO = (float) distance%400;
        float targetPosition = (float) Math.max(0, Math.min(1, distance));
        if (distance >= 0 && distance <= 400) {
            ValueAnimator animator = ValueAnimator.ofFloat(SPEED_RATIO,targetPosition);
            animator.setDuration(500); // 动画持续时间，单位毫秒
            animator.addUpdateListener(animation -> {
                SPEED_RATIO = (float) animation.getAnimatedValue();
                invalidate(); // 触发重绘
            });
            animator.start();
        }
//        invalidate();
    }

    private void init(){
        //进度点画笔
        mPaintProgressPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintProgressPoint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaintProgressPoint.setColor(DEFAULT_ORANGE_COLOR);
        mPaintProgressPoint.setStrokeWidth(16);
        //进度点画笔
//        mPaintProgressPoint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
//        mPaintProgressPoint1.setStyle(Paint.Style.FILL_AND_STROKE);
//        mPaintProgressPoint1.setColor(DEFAULT_INNER_ARC_COLOR);
//        mPaintProgressPoint1.setStrokeWidth(16);
        //进度点画笔
//        mPaintProgressPoint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
//        mPaintProgressPoint2.setStyle(Paint.Style.FILL_AND_STROKE);
//        mPaintProgressPoint2.setColor(DEFAULT_INNER_ARC_COLOR);
//        mPaintProgressPoint2.setStrokeWidth(16);
        //进度点画笔
//        mPaintProgressPoint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
//        mPaintProgressPoint3.setStyle(Paint.Style.FILL_AND_STROKE);
//        mPaintProgressPoint3.setColor(DEFAULT_INNER_ARC_COLOR);
//        mPaintProgressPoint3.setStrokeWidth(16);
        //进度点画笔
//        mPaintProgressPoint4 = new Paint(Paint.ANTI_ALIAS_FLAG);
//        mPaintProgressPoint4.setStyle(Paint.Style.FILL_AND_STROKE);
//        mPaintProgressPoint4.setColor(DEFAULT_INNER_ARC_COLOR);
//        mPaintProgressPoint4.setStrokeWidth(16);


        contourPaint = new Paint();
        contourPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        contourPaint.setStyle(Paint.Style.STROKE);
        contourPaint.setColor(Color.TRANSPARENT);
//        contourPaint.setColor(DEFAULT_TransColor);
        contourPaint.setStrokeWidth(dp2px(20));

//        Random random = new Random();
//        int i = random.nextInt(30)+10;
//        distanceRatio2 = i/10*100;
//        int i1 = random.nextInt(30)+10;
//        distanceRatio3 = i1/10*100;
//        int i2 = random.nextInt(30)+10;
//        distanceRatio4 = i2/10*100;
//        int i3 = random.nextInt(30)+10;
//        distanceRatio5 = i3/10*100;

        carPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        carPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
//        carPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }


    /**
     * dp2px
     */
    protected int dp2px(float dpValue) {
        float density = getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5f);
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.TRANSPARENT);

        int width = canvas.getWidth();
        int height = canvas.getHeight();
        canvas.translate(width / 2, height / 2); //移动canvas坐标系
        Path path = new Path();
//        path.moveTo(-(width / 8+width / 16), - (width / 8));
//        path.lineTo((width / 8+width / 16), - (width / 8));
//        //画条三阶贝塞尔曲线
//        path.cubicTo((width / 8+width / 16), -(width / 8),width / 4+width / 16,- (width / 8), width / 4+width / 16,0);//右上半圆弧
//        path.cubicTo(width / 4+width / 16, 0,width / 4+width / 16, width /8, (width / 8+width / 16),width / 8);//右下半圆弧
//        path.lineTo(-(width / 8+width / 16), width / 8);
//        //画条三阶贝塞尔曲线
//        path.cubicTo(-(width / 8+width / 16), width / 8, -(width / 4+width / 16), width / 8, -(width / 4+width / 16), 0); //左上半圆弧
//        path.cubicTo(-(width / 4+width / 16), 0, -(width / 4+width / 16), -(width / 8), -(width / 8+width / 16), - (width / 8)); //左下半圆弧


        path.moveTo(-(width / 6+width / 8), -(Math.round(width / 5.4) - dp2px(20)));
        path.lineTo((width / 6+width / 8), -(Math.round(width / 5.4) - dp2px(20)));


        //画条三阶贝塞尔曲线
        path.cubicTo((width / 6+width / 8), -(Math.round(width / 5.4) - dp2px(20)),Math.round(width / 2.5)+width / 16,- (Math.round(width / 5.4) - dp2px(20)), Math.round(width / 2.5)+width / 16,0);//右上半圆弧
        path.cubicTo(Math.round(width / 2.5)+width / 16, 0,(Math.round(width / 2.5)+width / 16), (Math.round(width / 5.4) - dp2px(20)), (width / 6+width / 8),(Math.round(width / 5.4) - dp2px(20)));//右下半圆弧
        path.lineTo(-(width / 6+width / 8), (Math.round(width / 5.4) - dp2px(20)));
        //画条三阶贝塞尔曲线
        path.cubicTo(-(width / 6+width / 8), (Math.round(width / 5.4) - dp2px(20)), -(Math.round(width / 2.5)+width / 16), (Math.round(width / 5.4) - dp2px(20)), -(Math.round(width / 2.5)+width / 16), 0); //左上半圆弧
        path.cubicTo(-(Math.round(width / 2.5)+width / 16), 0, -(Math.round(width / 2.5)+width / 16), -(Math.round(width / 5.4) - dp2px(20)), -(width / 6+width / 8), - (Math.round(width / 5.4) - dp2px(20))); //左下半圆弧



//        if(distanceRatio4>=100&&distanceRatio4<=100+SPEED_RATIO3){
//        }else{
//            distanceRatio4 += SPEED_RATIO3;
////            time++;
//        }
//        distanceRatio2 += SPEED_RATIO1;
//        distanceRatio3 += SPEED_RATIO2;
        distanceRatio = SPEED_RATIO;
//        distanceRatio5 += SPEED_RATIO4;
//        distanceRatio4 += SPEED_RATIO3;

        if(distanceRatio >=400f){
//        if(distanceRatio >=400){
            distanceRatio = 0;
        }
//        if(distanceRatio2 >=400){
//            distanceRatio2 = 0;
//            index++;
////            Toast.makeText(mContext,index+"",Toast.LENGTH_SHORT).show();
//            if(index==1){
//                distanceRatio4 += SPEED_RATIO3;
//                index=0;
//            }
//        }
//        if(distanceRatio3 >=400){
//            distanceRatio3 = 0;
//        }
//        if(distanceRatio4 >=400){
//            distanceRatio4 = 0;
//        }
//        if(distanceRatio5 >=400){
//            distanceRatio5 = 0;
//        }
        /**if(distanceRatio>=100){
            Random random = new Random();
            int i = random.nextInt(10);
            if(i==0){
                i=1;
            }
            SPEED_RATIO = 0.04f*i;
        }*/
//        if(distanceRatio2>=100){
//            Random random = new Random();
//            boolean probability = (int) (Math.random() * 4) == 0;
//            if(probability){
//                int i = random.nextInt(35-15+1)+ 15;//15-35随机数 int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
//                SPEED_RATIO1 = 0.04f*i/10;
//            }else{
//                int i = random.nextInt(15-5+1)+ 5;//5-15随机数 int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
//                SPEED_RATIO1 = 0.04f*i/10;
//            }
////            SPEED_RATIO = 0.0001f*i;
//        }
//        if(distanceRatio3>=100){
//            Random random = new Random();
//            boolean probability = (int) (Math.random() * 4) == 0;//0-4随机生成其中一个数，五分之一概率到速度快的
//            if(probability){
//                int i = random.nextInt(35-15+1)+ 15;//15-35随机数 int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
//                SPEED_RATIO2 = 0.04f*i/10;
//            }else{
//                int i = random.nextInt(15-5+1)+ 5;//5-15随机数 int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
//                SPEED_RATIO2 = 0.04f*i/10;
//            }
//        }
//        if(distanceRatio4>=100){
//            Random random = new Random();
//            boolean probability = (int) (Math.random() * 4) == 0;//0-4随机生成其中一个数，五分之一概率到速度快的
//            if(probability){
//                int i = random.nextInt(35-15+1)+ 15;//15-35随机数 int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
//                SPEED_RATIO3 = 0.04f*i/10;
//            }else{
//                int i = random.nextInt(15-5+1)+ 5;//5-15随机数 int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
//                SPEED_RATIO3 = 0.04f*i/10;
//            }
//        }
//        if(distanceRatio5>=100){
//            Random random = new Random();
//            boolean probability = (int) (Math.random() * 4) == 0;//0-4随机生成其中一个数，五分之一概率到速度快的
//            if(probability){
//                int i = random.nextInt(35-15+1)+ 15;//15-35随机数 int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
//                SPEED_RATIO4 = 0.04f*i/10;
//            }else{
//                int i = random.nextInt(15-5+1)+ 5;//5-15随机数 int randNumber =rand.nextInt(MAX - MIN + 1) + MIN;
//                SPEED_RATIO4 = 0.04f*i/10;
//            }
//        }
        PathMeasure pathMeasure = new PathMeasure(path, false);
//        PathMeasure pathMeasure1 = new PathMeasure(path, false);
//        PathMeasure pathMeasure2 = new PathMeasure(path, false);
//        PathMeasure pathMeasure3 = new PathMeasure(path, false);
//        PathMeasure pathMeasure4 = new PathMeasure(path, false);

        float distance = pathMeasure.getLength() * distanceRatio/400;
        pathMeasure.getPosTan(distance, mPos, null);

//        float distance2 = pathMeasure1.getLength() * distanceRatio3/400;
//        pathMeasure1.getPosTan(distance2, mPos1, null);
//        float distance1 = pathMeasure2.getLength() * distanceRatio2/400;
//        pathMeasure2.getPosTan(distance1, mPos2, null);
//        float distance3 = pathMeasure2.getLength() * distanceRatio4/400;
//        pathMeasure3.getPosTan(distance3, mPos3, null);
//        float distance4 = pathMeasure2.getLength() * distanceRatio5/400;
//        pathMeasure4.getPosTan(distance4, mPos4, null);
        mProgressPointRadius = dp2px(3);
        mPaintProgressPoint.setColor(Color.parseColor("#fe6b00"));

//        mPaintProgressPoint1.setColor(Color.parseColor("#ffffff"));
//        mPaintProgressPoint2.setColor(Color.parseColor("#ffffff"));
//        mPaintProgressPoint3.setColor(Color.parseColor("#ffffff"));
//        mPaintProgressPoint4.setColor(Color.parseColor("#ffffff"));

//        Matrix carMatrix = new Matrix();
//        Matrix carMatrix1 = new Matrix();
//        Matrix carMatrix2 = new Matrix();
        //获取距离的坐标和旋转角度
//        pathMeasure.getMatrix(distance, carMatrix, PathMeasure.TANGENT_MATRIX_FLAG | PathMeasure.POSITION_MATRIX_FLAG);
//        pathMeasure1.getMatrix(distance2, carMatrix2, PathMeasure.TANGENT_MATRIX_FLAG | PathMeasure.POSITION_MATRIX_FLAG);
//        pathMeasure2.getMatrix(distance1, carMatrix1, PathMeasure.TANGENT_MATRIX_FLAG | PathMeasure.POSITION_MATRIX_FLAG);
        //这里要将设置到小车的中心点
//        carMatrix.preTranslate(-carBitmap.getWidth() / 2, -carBitmap.getHeight() / 2);
//        carMatrix1.preTranslate(-carBitmap.getWidth() / 2, -carBitmap.getHeight() / 2);
//        carMatrix2.preTranslate(-carBitmap.getWidth() / 2, -carBitmap.getHeight() / 2);

        canvas.drawPath(path, contourPaint); //先画轨迹
        //绘制进度点
        if(mPos[0] != 0 && mPos[1] != 0) {
            canvas.drawCircle(mPos[0], mPos[1], mProgressPointRadius, mPaintProgressPoint);
        }
//        //绘制进度点
//        if(mPos1[0] != 0 && mPos1[1] != 0) {
//            canvas.drawCircle(mPos1[0], mPos1[1], mProgressPointRadius, mPaintProgressPoint1);
//        }
//        //绘制进度点
//        if(mPos2[0] != 0 && mPos2[1] != 0) {
//            canvas.drawCircle(mPos2[0], mPos2[1], mProgressPointRadius, mPaintProgressPoint2);
//        }
//        //绘制进度点
//        if(mPos3[0] != 0 && mPos3[1] != 0) {
//            canvas.drawCircle(mPos3[0], mPos3[1], mProgressPointRadius, mPaintProgressPoint3);
//        }
//        //绘制进度点
//        if(mPos4[0] != 0 && mPos4[1] != 0) {
//            canvas.drawCircle(mPos4[0], mPos4[1], mProgressPointRadius, mPaintProgressPoint4);
//        }

//        canvas.drawBitmap(carBitmap, carMatrix, carPaint);
//        canvas.drawBitmap(carBitmap2, carMatrix1, carPaint1);
//        canvas.drawBitmap(carBitmap2, carMatrix2, carPaint2);

//        invalidate();
    }
}
